/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package SQL2003.impl;

import SQL2003.Restriction;
import SQL2003.SQL2003Package;
import SQL2003.StructuralComponent;
import SQL2003.Table;
import SQL2003.util.derivedAttributeAdapter;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectWithInverseResolvingEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Restriction</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link SQL2003.impl.RestrictionImpl#getTable <em>Table</em>}</li>
 *   <li>{@link SQL2003.impl.RestrictionImpl#getColumns <em>Columns</em>}</li>
 *   <li>{@link SQL2003.impl.RestrictionImpl#getNameColumns <em>Name Columns</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public abstract class RestrictionImpl extends EObjectImpl implements Restriction {
	/**
	 * The cached value of the '{@link #getColumns() <em>Columns</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getColumns()
	 * @generated
	 * @ordered
	 */
	protected EList<StructuralComponent> columns;

	/**
	 * The default value of the '{@link #getNameColumns() <em>Name Columns</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getNameColumns()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_COLUMNS_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getNameColumns() <em>Name Columns</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getNameColumns()
	 * @generated
	 * @ordered
	 */
	protected String nameColumns = NAME_COLUMNS_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * 
	 */
	protected RestrictionImpl() {
		super();
		derivedAttributeAdapter daa = new derivedAttributeAdapter(this, SQL2003Package.RESTRICTION__NAME_COLUMNS);
		daa.addNavigatedDependency(SQL2003Package.RESTRICTION__COLUMNS, SQL2003Package.STRUCTURAL_COMPONENT__NAME);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return SQL2003Package.Literals.RESTRICTION;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Table getTable() {
		if (eContainerFeatureID != SQL2003Package.RESTRICTION__TABLE) return null;
		return (Table)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetTable(Table newTable, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newTable, SQL2003Package.RESTRICTION__TABLE, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setTable(Table newTable) {
		if (newTable != eInternalContainer() || (eContainerFeatureID != SQL2003Package.RESTRICTION__TABLE && newTable != null)) {
			if (EcoreUtil.isAncestor(this, newTable))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newTable != null)
				msgs = ((InternalEObject)newTable).eInverseAdd(this, SQL2003Package.TABLE__RESTRICTIONS, Table.class, msgs);
			msgs = basicSetTable(newTable, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SQL2003Package.RESTRICTION__TABLE, newTable, newTable));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<StructuralComponent> getColumns() {
		if (columns == null) {
			columns = new EObjectWithInverseResolvingEList.ManyInverse<StructuralComponent>(StructuralComponent.class, this, SQL2003Package.RESTRICTION__COLUMNS, SQL2003Package.STRUCTURAL_COMPONENT__RESTRICTIONS);
		}
		return columns;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 *@generated NOT
	 *
	 */
	public String getNameColumns() {
		String nNombre=new String("");
		if(this.columns!=null){
			for(int i=0; i<this.columns.size();i++){
				if(i==this.columns.size()-1){ 
					nNombre=nNombre+this.columns.get(i).getName();
				}else{
					nNombre=nNombre+this.columns.get(i).getName()+", ";
				}
			}
		}
		nameColumns=nNombre;
		return nameColumns;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 * 
	 */
	public void setNameColumns(String newNameColumns) {
		String oldNameColumns = nameColumns;
		//nameColumns = newNameColumns;
		String nNombre=new String("");
		if (this.columns != null) {
			for (int i = 0; i < this.columns.size(); i++) {
				if (i == this.columns.size() - 1) {
					nNombre = nNombre + this.columns.get(i).getName();
				} else {
					nNombre = nNombre + this.columns.get(i).getName() + ", ";
				}
			}
		}
		nameColumns=nNombre;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SQL2003Package.RESTRICTION__NAME_COLUMNS, oldNameColumns, nameColumns));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * 
	 */
	public void setNombre(String newNombre) {
		String oldNombre = nameColumns;
		//nombre = newNombre;
		String nNombre=new String("");
		for(int i=0; i<this.columns.size();i++){
			nNombre=nNombre+this.columns.get(i).getName()+", ";
		}
		nameColumns=nNombre;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SQL2003Package.RESTRICTION__NAME_COLUMNS, oldNombre, nameColumns));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case SQL2003Package.RESTRICTION__TABLE:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetTable((Table)otherEnd, msgs);
			case SQL2003Package.RESTRICTION__COLUMNS:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getColumns()).basicAdd(otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case SQL2003Package.RESTRICTION__TABLE:
				return basicSetTable(null, msgs);
			case SQL2003Package.RESTRICTION__COLUMNS:
				return ((InternalEList<?>)getColumns()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID) {
			case SQL2003Package.RESTRICTION__TABLE:
				return eInternalContainer().eInverseRemove(this, SQL2003Package.TABLE__RESTRICTIONS, Table.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case SQL2003Package.RESTRICTION__TABLE:
				return getTable();
			case SQL2003Package.RESTRICTION__COLUMNS:
				return getColumns();
			case SQL2003Package.RESTRICTION__NAME_COLUMNS:
				return getNameColumns();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case SQL2003Package.RESTRICTION__TABLE:
				setTable((Table)newValue);
				return;
			case SQL2003Package.RESTRICTION__COLUMNS:
				getColumns().clear();
				getColumns().addAll((Collection<? extends StructuralComponent>)newValue);
				return;
			case SQL2003Package.RESTRICTION__NAME_COLUMNS:
				setNameColumns((String)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case SQL2003Package.RESTRICTION__TABLE:
				setTable((Table)null);
				return;
			case SQL2003Package.RESTRICTION__COLUMNS:
				getColumns().clear();
				return;
			case SQL2003Package.RESTRICTION__NAME_COLUMNS:
				setNameColumns(NAME_COLUMNS_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case SQL2003Package.RESTRICTION__TABLE:
				return getTable() != null;
			case SQL2003Package.RESTRICTION__COLUMNS:
				return columns != null && !columns.isEmpty();
			case SQL2003Package.RESTRICTION__NAME_COLUMNS:
				return NAME_COLUMNS_EDEFAULT == null ? nameColumns != null : !NAME_COLUMNS_EDEFAULT.equals(nameColumns);
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (NameColumns: ");
		result.append(nameColumns);
		result.append(')');
		return result.toString();
	}

} //RestrictionImpl
